package com.jhgk.jhcee.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhgk.jhcee.common.domain.TableListModel;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import com.jhgk.jhcee.data.auth.entity.SysOrgInfo;
import com.jhgk.jhcee.data.auth.mapper.SysOrgInfoMapper;
import com.jhgk.jhcee.system.domain.dao.SysOrgInfoDao;
import com.jhgk.jhcee.system.domain.model.SysMenuMetaRelationModel;
import com.jhgk.jhcee.system.domain.model.SysMenuMetaRelationModelComparator;
import com.jhgk.jhcee.system.domain.model.SysOrgInfoModel;
import com.jhgk.jhcee.system.service.SysOrgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @Description:
 * @Author: lemon_xzp
 * @Date: 2021/9/23 7:57
 */
@Service(value = "SysOrgInfoServiceImpl")
public class SysOrgInfoServiceImpl extends ServiceImpl<SysOrgInfoMapper, SysOrgInfo> implements SysOrgInfoService {

    @Autowired
    SysOrgInfoMapper sysOrgInfoMapper;

    @Autowired
    SysOrgInfoDao sysOrgInfoDao;

    @Override
    public BasisResultful<Object> getOrgAllListPage(SysOrgInfoModel model) {
        List<SysOrgInfoModel> sysOrgInfos = sysOrgInfoDao.selectListAll();
        PageHelper.startPage(model.getPage(), model.getPageSize());
        List<SysOrgInfoModel> parentOrg = sysOrgInfoDao.searchParentOrg();
        parentOrg.stream().forEach(parent -> {
            List<SysOrgInfoModel> children = getChildren(parent.getOrgId(), sysOrgInfos);
            parent.setChildren(children);
        });
        sysOrgInfos.sort(Comparator.comparing(SysOrgInfo::getSort));
        PageInfo<SysOrgInfoModel> pageInfo = new PageInfo<>(sysOrgInfos);
        return BasisResultful.success(new TableListModel(pageInfo.getList(), pageInfo.getTotal()));
    }

    @Override
    public BasisResultful<Object> getAllList() {
        List<SysOrgInfoModel> sysOrgInfos = sysOrgInfoDao.selectListAll();
        List<SysOrgInfoModel> parentOrg = sysOrgInfoDao.searchParentOrg();
        parentOrg.stream().forEach(parent -> {
            List<SysOrgInfoModel> children = getChildren(parent.getOrgId(), sysOrgInfos);
            parent.setChildren(children);
        });
        sysOrgInfos.sort(Comparator.comparing(SysOrgInfo::getSort));
        return BasisResultful.success(parentOrg);
    }

    @Override
    public BasisResultful<Object> addOrg(SysOrgInfoModel model) {
        if (null == model) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        boolean save = this.save(model);
        return save ? BasisResultful.success() : BasisResultful.fail();
    }

    @Override
    public BasisResultful<Object> updateOrg(SysOrgInfoModel model) {
        if (null == model) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        this.saveOrUpdate(model);
        return BasisResultful.success();
    }

    @Override
    public BasisResultful<Object> deleteOrg(SysOrgInfoModel model) {
        if (null == model) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        QueryWrapper<SysOrgInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("org_id", model.getOrgId());
        int delete = sysOrgInfoMapper.delete(wrapper);
        return delete > 0 ? BasisResultful.success() : BasisResultful.fail();
    }

    @Override
    public BasisResultful<Object> updateStatus(Long orgId, Integer status) {
        Integer integer = sysOrgInfoMapper.updateStatus(orgId, status);
        return integer > 0 ? BasisResultful.success() : BasisResultful.fail();
    }

    private List<SysOrgInfoModel> getChildren(Long parentId, List<SysOrgInfoModel> rootOrg) {
        // 子菜单
        List<SysOrgInfoModel> childList = new ArrayList<>();
        rootOrg.stream().forEach(org -> {
            if (null != org.getOrgParentId()) {
                if (org.getOrgParentId() == parentId) {
                    childList.add(org);
                }
            }
        });
        childList.sort(Comparator.comparing(SysOrgInfoModel::getSort));
        childList.stream().forEach(org -> {
            org.setChildren(getChildren(org.getOrgId(), rootOrg));
        });
        // 推出递归
        if (0 == childList.size()) {
            return null;
        }
        return childList;
    }
}
